package com.zhuiyun.project.api.group.mapper;

import com.zhuiyun.project.api.appuser.entity.User;
import com.zhuiyun.project.api.friend.entity.Friend;
import com.zhuiyun.project.api.group.entity.Group;
import com.zhuiyun.project.api.group.model.GroupModel;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @ClassName GroupMapper
 * @Description TODO
 * @Author kdj
 * @Date 2023/7/24 10:00
 **/
@Mapper
public interface GroupMapper extends BaseMapper<Group> {
    /**
     * @param groupNum
     * @Author kdj
     * @Description 查询该群号出现过几次
     * @Date 2023/7/24 11:37
     * @Return int
     */
    @Select("select count(*) from group_chat where group_num=#{groupNum}")
    int queryNumIsOne(Long groupNum);

    /**
     * @param createUserId
     * @Author kdj
     * @Description 获取群聊根据创始人
     * @Date 2023/7/24 16:04
     * @Return java.util.List<com.zhuiyun.project.api.group.entity.Group>
     */
    @Select("select * from group_chat where create_user_id=#{createUserId}")
    List<Group> getGroupByCreateUserId(int createUserId);

    /**
     * @param groupId
     * @Author kdj
     * @Description 根据groupId获取group对象
     * @Date 2023/7/24 19:56
     * @Return com.zhuiyun.project.api.group.entity.Group
     */
    @Select("select * from group_chat where id=#{groupId}")
    Group getGroupByGroupId(int groupId);

    /**
     * @param groupNum
     * @Author kdj
     * @Description 根据群号搜索到群
     * @Date 2023/7/25 20:29
     * @Return com.zhuiyun.project.api.group.entity.Group
     */
    @Select("select * from group_chat where group_num=#{groupNum}")
    Group queryGroupByNum(int groupNum);

    /**
     * @param groupModel
     * @Author 韩佳轩
     * @Description 群聊首页信息
     * @Date 2023/7/26 21:48
     * @Return
     */
    @Select("SELECT g.*,s.dict_label as dictLabel from group_chat g,sys_dict_data s where g.disease_id=s.data_id and id=#{groupId}")
    List<GroupModel> queryAllGroup(GroupModel groupModel);

    /**
     * @param group
     * @Author 韩佳轩
     * @Description 修改头像和群名称
     * @Date 2023/7/27 19:26
     * @Return
     */
    @Update("update group_chat set group_name=#{groupName},group_img_id=#{groupImgId} where id=#{id}")
    int updateGroupAll(Group group);

    /**
     * @param groupId
     * @Author kdj
     * @Description
     * @Date 2023/7/27 19:49
     * @Return int
     */
    @Select("select create_user_id from group_chat where id=#{groupId}")
    int getCreateUserId(int groupId);

    /**
     * 查询群信息
     *
     * @return com.zhuiyun.project.api.group.model.GroupModel
     * @author 温
     * @date 2023/7/31 19:04
     * @parameter [groupId]
     */
    @Select("select distinct aa.*,s.dict_label,msg.is_muted from group_chat aa,sys_dict_data s,message_list msg  " +
            "where aa.del_flag=0 and aa.disease_id=s.data_id and aa.id=#{groupId}" +
            " and msg.type=2 and msg.from_id=aa.id and msg.to_id=#{userId}")
    GroupModel Group(int groupId, int userId);

    /**
     * 查询群成员
     *
     * @return java.util.List<com.zhuiyun.project.api.appuser.entity.User>
     * @author 温
     * @date 2023/7/31 19:03
     * @parameter [groupId]
     */
    @Select("SELECT a.* FROM join_group m,app_user a where a.id=m.user_id  and m.group_id=#{groupId} and m.join_status=1  and m.del_flag=0")
    List<User> userss(int groupId);

    /**
     * 查询群成员总数
     *
     * @return java.util.List<com.zhuiyun.project.api.appuser.entity.User>
     * @author 杨珅
     * @date 2023/7/31 19:03
     * @parameter [groupId]
     */
    @Select("SELECT count(*) FROM join_group m,app_user a where a.id=m.user_id  and m.group_id=#{groupId} and m.join_status=1  and m.del_flag=0")
    int userCount(int groupId);
}
